package com.zprmb.udzt;

import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.adapter.InfoAdapter;
import com.zprmb.udzt.bean.Info;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

/**
 * 资讯
 * 
 * @author shenshao
 * 
 */
public class Information extends BaseActivity implements OnClickListener,
		OnRefreshListener2<ListView> {
	private PullToRefreshListView lv_product_fragment;
	private int page = 1;
	private ListView actualListView;
	private ArrayList<Info> list = new ArrayList<Info>();
	private ArrayList<Info> list2 = new ArrayList<Info>();
	private InfoAdapter adapter;
	private ImageView img_back;
	private RelativeLayout rl_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.information);
		BbtApplication.getInstance().addActvity(this);
		lv_product_fragment = (PullToRefreshListView) findViewById(R.id.lv);
		actualListView = lv_product_fragment.getRefreshableView();
		registerForContextMenu(actualListView);
		img_back = (ImageView) findViewById(R.id.img_back);
		img_back.setOnClickListener(this);
		rl_back = (RelativeLayout) findViewById(R.id.rl_back);
		rl_back.setOnClickListener(this);
		lv_product_fragment.setOnRefreshListener(this);
		lv_product_fragment.setMode(Mode.BOTH);
		lv_product_fragment.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Information.this,
						V4WebViewActivity.class);
				intent.putExtra("url", "/api/v3/articleDetail?cmsId="
						+ list2.get(position - 1).getCmsId() + "app=true");
				intent.putExtra("title", "资讯信息");
				startActivity(intent);
			}
		});
		initData();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}

	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("资讯", jsonObject.toString());
				Gson gson = new Gson();
				if (!jsonObject.optString("resultList").equals("null")) {
					list = gson.fromJson(jsonObject.optString("resultList"),
							new TypeToken<ArrayList<Info>>() {
							}.getType());

				}
				list2.addAll(list);
				if (list2.get(3).getTitleImageUrl() == null) {
					Log.e("asas", "222222");
				}
				adapter = new InfoAdapter(list2, Information.this);
				lv_product_fragment.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				actualListView.setSelection(list2.size() - 10);
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(Information.this, Constant.URL
				+ "/api/v5/app/zx?pageNo=" + page + "&pageSize=10", "");
		Log.e("info", Constant.URL + "/api/v5/app/zx?pageNo=" + page
				+ "&pageSize=10");
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(2);
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			list.clear();
			if (msg.what == 1) {
				page = 1;
				list2.clear();
				initData();
			} else {
				page += 1;
				initData();

			}
			Toast.makeText(Information.this, "刷新成功", 2000).show();
			lv_product_fragment.onRefreshComplete();
		};
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.img_back:
			finish();
			break;
		case R.id.rl_back:
			finish();
			break;
		}
	}

}
